package com.zhxh.nzpos.stock;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

import com.zhxh.nzpos.BaseActivity;
import com.zhxh.nzpos.R;
import com.zhxh.nzpos.dao.CategoryDao;
import com.zhxh.nzpos.dao.StockDao;
import com.zhxh.nzpos.model.Category;
import com.zhxh.nzpos.model.Stock;
import com.zhxh.nzpos.utils.ViewUtil;

/**
 * 库存盘点界面
 * 
 * @author wangzt
 *
 */
public class StockManageActivity extends BaseActivity {

	private static final int SEARCH_SUCCESS = 1;
	private static final int SEARCH_FAIL = 2;
	
	private ArrayList<Stock> stocks;//显示库存列表
	private StockListAdapter listAdapter;
	private StockDao stockDao;
	
	private String[] category_items;
	List<Category> list;
	
	private int selected = 0;
	
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == SEARCH_SUCCESS) {
				listAdapter.notifyDataSetChanged();
			}
			ViewUtil.cancelProgressDialog();
		}
		
	};
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stock_manage);
        stockDao = new StockDao(getApplicationContext());
        initViews();
    }
	
	private void initViews() {
		CategoryDao cateDao = new CategoryDao(this);
		list =  cateDao.getAll();
		category_items = new String[list.size()+1];
		category_items[0] = "全部类别";
		for(int i = 0 ; i < list.size() ;i++) {
			Category cate = list.get(i);
			category_items[i+1] = cate.getCategory();
		}
		
		Spinner typeSpinner = (Spinner) findViewById(R.id.goods_type_spinner);
//		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
//				getApplicationContext(), R.array.stock_type,
//				R.layout.custom_spinner_text);
//		adapter.setDropDownViewResource(R.layout.custom_spinner_dropdown);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,category_items);
		 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		typeSpinner.setAdapter(adapter);
		stocks = new ArrayList<Stock>();
		stocks.addAll(stockDao.getAll());
		typeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					final int position, long id) {
				if (position != selected) {
					ViewUtil.showProgressDialog(StockManageActivity.this,
							"正在查询...");
					new Thread() {
						public void run() {
							boolean handle = false;
							if (position == 0) {
								stocks.clear();
								stocks.addAll(stockDao.getAll());
								handle = true;
								handler.sendEmptyMessage(SEARCH_SUCCESS);
							} else {
								Category cate = list.get(position - 1);
								stocks.clear();
								stocks.addAll(stockDao.findByTypeId(cate
										.getId()));
								handle = true;
								handler.sendEmptyMessage(SEARCH_SUCCESS);
							}
							if (!handle) {
								handler.sendEmptyMessage(SEARCH_FAIL);
							}
						}

					}.start();

				}
			}
			public void onNothingSelected(AdapterView<?> parent) {
				// ignore
			}
		});
		
		ListView listView = (ListView) findViewById(R.id.stock_list);
		listAdapter = new StockListAdapter(getLayoutInflater(), stocks);
		listView.setAdapter(listAdapter);
		
/*		Button exitBtn = (Button) findViewById(R.id.stock_exit_btn);
		exitBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});*/
	}
}
